<template>
  <section>
    <el-row>
      <el-col :span="24">
        <!--表单-->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="选课">
            <el-select v-model="formInline.classrecord.chooseCourseId" filterable placeholder="请选择">
              <el-option
                v-for="item in chooseCourses"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleEdit">新增</el-button>
        </el-form>
        <!--表格-->
        <el-table
          :data="tableData"
          height="500"
          border
          style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="签到编号">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="选课编号">
                  <span>{{ props.row.chooseCourseId }}</span>
                </el-form-item>
                <el-form-item label="学生姓名">
                  <span>{{ props.row.studentName }}</span>
                </el-form-item>
                <el-form-item label="课程名称">
                  <span>{{ props.row.courseName }}</span>
                </el-form-item>
                <el-form-item label="上次结转剩余总课时">
                  <span>{{ props.row.lastCarryOverTotalRemainClass }}</span>
                </el-form-item>
                <el-form-item label="上次结转剩余缴费课时">
                  <span>{{ props.row.lastCarryOverRemainClass }}</span>
                </el-form-item>
                <el-form-item label="上次结转剩余赠送课时">
                  <span>{{ props.row.lastCarryOverRemainGiveClassHourCount }}</span>
                </el-form-item>
                <el-form-item label="剩余总课时">
                  <span>{{ props.row.remainTotalClassHourCount }}</span>
                </el-form-item>
                <el-form-item label="剩余缴费课时">
                  <span>{{ props.row.remainClass }}</span>
                </el-form-item>
                <el-form-item label="剩余赠送课时">
                  <span>{{ props.row.remainGiveClassHourCount }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column type="selection">
          </el-table-column>
          <el-table-column
            prop="id"
            label="编号"
            width="90">
          </el-table-column>
          <el-table-column
            prop="studentName"
            label="学生姓名"
            width="90">
          </el-table-column>
          <el-table-column
            prop="courseName"
            label="课程名称"
            width="90">
          </el-table-column>
          <el-table-column
            prop="lastCarryOverTotalRemainClass"
            label="上次结转剩余总课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="lastCarryOverRemainClass"
            label="上次结转剩余缴费课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="lastCarryOverRemainGiveClassHourCount"
            label="上次结转剩余赠送课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="remainTotalClassHourCount"
            label="剩余总课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="remainClass"
            label="剩余缴费课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="remainGiveClassHourCount"
            label="剩余赠送课时"
            width="90">
          </el-table-column>
          <el-table-column
            prop="classStartTime"
            label="开始时间"
            width="200">
            <template slot-scope="scope">
              {{scope.row.classStartTime|datefmt("YYYY-MM-DD HH:mm:ss")}}
            </template>
          </el-table-column>
          <el-table-column
            prop="classEndTime"
            label="结束时间"
            width="200">
            <template slot-scope="scope">
              {{scope.row.classEndTime|datefmt("YYYY-MM-DD HH:mm:ss")}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template scope="scope">
              <el-button type="primary" class="el-icon-edit" size="small" @click="handleEdit(scope.$index, scope.row)">
                编辑
              </el-button>
              <el-button type="danger" class="el-icon-delete" size="small"
                         @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            :page-sizes="[4, 8, 16]"
            layout="total,sizes,prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialogFormVisible" title="修改签到" :visible="dialogFormVisible" size="tiny"
               :before-close="handleClose">
      <el-form ref="classrecord" :model="classrecord" label-width="80px">
        <el-form-item label="选课信息">
          <el-select v-model="classrecord.chooseCourseId" filterable placeholder="请选择">
            <el-option
              v-for="item in chooseCourses"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上课时间">
          <el-date-picker
            v-model="value5"
            type="datetimerange"
            :picker-options="pickerOptions2"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave" :loading="editLoading">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
  const ERR_OK = "success";
  const ERR_FAIL = "fail";

  export default {
    data() {
      return {
        formInline: {
          classrecord: {
            chooseCourseId: '',
          }
        },
        tableData: [],
        options: [],
        places: [],
        dialogFormVisible: false,
        editLoading: false,
        classrecord: {
          id: '',
          chooseCourseId: '',
          studentName: '',
          courseName: '',
          classStartTime: '',
          classEndTime: '',
          remainClass: '',
          remainGiveClassHourCount: '',
          remainTotalClassHourCount: '',
          lastCarryOverTotalRemainClass: '',
          lastCarryOverRemainGiveClassHourCount: '',
          lastCarryOverRemainClass: '',
        },
        currentPage: 1,
        pageSize: 4,
        total: 10,
        table_index: 999,
        chooseCourses: [],

        pickerOptions2: {
          shortcuts: [{
            text: '9:00-10:30',
            onClick(picker) {
              let end = new Date();
              end.setHours(10, 30, 0, 0);
              let start = new Date();
              start.setHours(9, 0, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '10:30-12:00',
            onClick(picker) {
              let end = new Date();
              end.setHours(12, 0, 0, 0);
              let start = new Date();
              start.setHours(10, 30, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '2:00-3:30',
            onClick(picker) {
              let end = new Date();
              end.setHours(15, 30, 0, 0);
              let start = new Date();
              start.setHours(14, 0, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '3:30-5:00',
            onClick(picker) {
              let end = new Date();
              end.setHours(17, 0, 0, 0);
              let start = new Date();
              start.setHours(15, 30, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '5:00-6:30',
            onClick(picker) {
              let end = new Date();
              end.setHours(18, 30, 0, 0);
              let start = new Date();
              start.setHours(17, 0, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '6:30-8:00',
            onClick(picker) {
              let end = new Date();
              end.setHours(20, 0, 0, 0);
              let start = new Date();
              start.setHours(18, 30, 0, 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        value5: []
      };
    },
    created() {
      this.handleQuery();
    },
    methods: {
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      handleQuery() {
        console.log("query invoke！！！");
        var param = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          chooseCourseId: this.formInline.classrecord.chooseCourseId,
        };
        this.$http.get('/class/record', {params: param}).then((response) => {
          response = response.data;
          if (response.status === ERR_OK) {
            this.tableData = response.data.classRecordInfos;
            this.total = response.data.total;
            this.chooseCourses = response.data.chooseCourses;
          } else {
            this.$message.error('操作失败');
          }
        });
      },
      handleDelete(index, row) {
        this.classrecord = Object.assign({}, row);
        this.$confirm('确认删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel'
        }).then(() => {
          var param = {id: this.classrecord.id}
          this.$http.delete('/class/record', {params: param}).then((response) => {
            response = response.data;
            if (response.status === ERR_OK) {
              this.$message({
                message: "操作成功！",
                type: 'success'
              });
              this.handleQuery();
            } else {
              this.$message.error('操作失败');
            }
          });
        }).catch(() => {
        });
      },
      handleEdit(index, row) {
        this.dialogFormVisible = true;
        this.classrecord = Object.assign({}, row);
        this.table_index = index;
        this.value5 = [this.classrecord.classStartTime, this.classrecord.classEndTime];
      },
      handleSave() {
        this.$confirm('确认修改吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          cancelButtonClass: 'cancel'
        }).then(() => {
          this.editLoading = true;
          // this.tableData.splice(this.table_index, 1, this.classrecord);
          this.classrecord.classStartTime = this.value5[0];
          this.classrecord.classEndTime = this.value5[1];
          if (this.classrecord.id != null) {
            this.$http.patch('/class/record', this.classrecord).then((response) => {
              response = response.data;
              if (response.status === ERR_OK) {
                this.$message({
                  message: "操作成功！",
                  type: 'success'
                });
                this.handleQuery();
              } else {
                this.$message.error('操作失败');
              }
            }).catch(() => {
              this.$message.error('操作失败');
            });
          } else {
            this.$http.post('/class/record', this.classrecord).then((response) => {
              response = response.data;
              console.log(response);
              if (response.status === ERR_OK) {
                this.$message({
                  message: "操作成功！",
                  type: 'success'
                });
                this.handleQuery();
              } else if (response.status === ERR_FAIL) {
                if (response.data.code === 100) {
                  this.$message.error('剩余课时不足,请缴费！');
                }
              } else {
                this.$message.error('操作失败');
              }
            }).catch((error) => {
              this.$message.error(error);
            });
          }
          this.editLoading = false;
          this.dialogFormVisible = false;
        }).catch();
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.handleQuery();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.handleQuery();
      }
    }
  };
</script>

<style scoped>

  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

  .el-pagination {
    text-align: center;
    margin-top: 30px;
  }

  .el-message-box__btns .cancel {
    float: right;
    margin-left: 10px;
  }
</style>
